package com.example.tse.rbyair.tsedemo;

import android.os.Bundle;
import android.widget.LinearLayout;

import com.example.tse.rbhttpservice.service.RemoteServiceFactory;
import com.example.tse.rbhttpservice.service.RemoteServiceListenerT;
import com.example.tse.rbhttpservice.service.account.model.AccountSendVcodeRequest;
import com.example.tse.rbhttpservice.service.account.model.AccountSendVcodeResponse;
import com.example.tse.rbhttpservice.service.activities.model.ActivitiesSeckillNoticeRequest;
import com.example.tse.rbhttpservice.service.activities.model.ActivitiesSeckillNoticeResponse;
import com.example.tse.rbhttpservice.service.common.model.CommonGetADList;
import com.example.tse.rbhttpservice.service.common.model.CommonGetADListRequest;
import com.example.tse.rbhttpservice.service.common.model.CommonGetADListResponse;
import com.example.tse.rbhttpservice.service.goods.model.GoodsGetSimilarList;
import com.example.tse.rbhttpservice.service.goods.model.GoodsGetSimilarListData;
import com.example.tse.rbhttpservice.service.goods.model.GoodsGetSimilarListRequest;
import com.example.tse.rbhttpservice.service.goods.model.GoodsGetSimilarListResponse;
import com.example.tse.rbhttpservice.service.home.model.HomeGetSecondBody;
import com.example.tse.rbhttpservice.service.home.model.HomeGetSecondRequest;
import com.example.tse.rbhttpservice.service.home.model.HomeGetSecondResponse;
import com.example.tse.rbhttpservice.service.home.model.HomeGetSpecials;
import com.example.tse.rbhttpservice.service.home.model.HomeGetSpecialsGoods;
import com.example.tse.rbhttpservice.service.home.model.HomeGetSpecialsRequest;
import com.example.tse.rbhttpservice.service.home.model.HomeGetSpecialsResponse;
import com.example.tse.rbyair.R;

import org.kymjs.kjframe.adapter.Common2TypeAdapter;
import org.kymjs.kjframe.adapter.ViewHolder;
import org.kymjs.kjframe.base.BaseActivity;
import org.kymjs.kjframe.log.L;
import org.kymjs.kjframe.utils.StringUtils;
import org.kymjs.kjframe.widget.KJListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by tse on 16/1/22.
 */
public class DemoListActivity extends BaseActivity {


    @Bind(R.id.list)
    KJListView list;
    @Bind(R.id.main_welecome_activity)
    LinearLayout mainWelecomeActivity;

    private Common2TypeAdapter adapter;
    private ArrayList<HomeGetSpecials> homeSpecialList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        L.d("DemoListActivity onCreate:");
        setContentView(R.layout.activity_main_demotse);
        ButterKnife.bind(this);



        list.setKJListViewListener(new KJListView.KJListViewListener() {
            @Override
            public void onRefresh() {
                L.d("lichun","DemoListActivity onRefresh:");
                list.stopPullRefresh();
            }

            @Override
            public void onLoadMore() {
                L.d("lichun","DemoListActivity onLoadMore:");
                list.stopLoadMore();
            }
        });






        homeSpecialList = new ArrayList<>();

        adapter = new Common2TypeAdapter<HomeGetSpecials>(getApplication(), homeSpecialList, R.layout.list_item_type_1, R.layout.list_item_type_2) {
            @Override
            public int getViewTypeCount() {
                return 2;
            }

            @Override
            public int getItemViewType(int position) {
                return position % 2;
            }

            @Override
            public void convert(ViewHolder holder, HomeGetSpecials homeGetSpecials, int position) {
                holder.setText(R.id.textView_type_1, position + ":" + homeGetSpecials.getSpecialName());
            }

            @Override
            public void convertType2(ViewHolder holder, HomeGetSpecials homeGetSpecials, int position) {
                holder.setText(R.id.textView_type_2, position + ":" + homeGetSpecials.getSpecialName());
            }
        };


        list.setAdapter(adapter);












        HomeGetSpecialsRequest re1 = new HomeGetSpecialsRequest() {
        };

        RemoteServiceFactory.getInstance().getHomeService(getApplication()).getSpecials(re1, new RemoteServiceListenerT<HomeGetSpecialsResponse>() {
            @Override
            public void okData(HomeGetSpecialsResponse homeGetSpecialsResponse, int fromWhere) {
                super.okData(homeGetSpecialsResponse, fromWhere);
                List<HomeGetSpecials> ss = homeGetSpecialsResponse.getGetSpecials();
                ArrayList<HomeGetSpecials> tmp = (ArrayList<HomeGetSpecials>) homeGetSpecialsResponse.getGetSpecials();

                homeSpecialList.clear();
                homeSpecialList.addAll(tmp);
                adapter.notifyDataSetChanged();
//                L.d("WelcomeActivity ss:" + ss.size() + " fromWhere:" + fromWhere);
                for (int i = 0; i < ss.size(); i++) {
                    HomeGetSpecials special = ss.get(i);
//                    L.d("DemoListActivity special.getSpecialName():" + special.getSpecialName());
                    List<HomeGetSpecialsGoods> gooList = special.getGoods();
                    for (int j = 0; j < gooList.size(); j++) {
                        HomeGetSpecialsGoods good = gooList.get(j);
//                        L.d("DemoListActivity   j:" + j + " getName:" + good.getName() + " getPrice:" + good.getPrice());
                    }
                }
            }
        });


    }


    private void testServerce() {

        /**
         *   发送验证码
         *   phone   :手机号
         *   type: 类型，1忘记密码、2用户注册
         */
        AccountSendVcodeRequest vcodeRequest = new AccountSendVcodeRequest() {
            @Override
            public String givePhone() {
                return "18202164667";
            }

            @Override
            public String giveType() {
                return "1";
            }
        };
        RemoteServiceFactory.getInstance().getAccountService(getApplication()).sendVcode(vcodeRequest, new RemoteServiceListenerT<AccountSendVcodeResponse>() {
            @Override
            public void okData(AccountSendVcodeResponse accountSendVcodeResponse) {
                super.okData(accountSendVcodeResponse);
//                accountSendVcodeResponse.

            }

            @Override
            public void onMessage(String message) {
                super.onMessage(message);
                L.d("DemoListActivity message:" + message);
            }
        });


        /**
         *   获取秒杀
         */

        HomeGetSecondRequest seconRequest = new HomeGetSecondRequest() {
        };
        RemoteServiceFactory.getInstance().getHomeService(getApplication()).getSecond(seconRequest, new RemoteServiceListenerT<HomeGetSecondResponse>() {


            @Override
            public void okData(HomeGetSecondResponse homeGetSecondResponse) {
                super.okData(homeGetSecondResponse);

                List<HomeGetSecondBody> secondList = homeGetSecondResponse.getGetSecond();

                L.d("DemoListActivity secondList size:" + secondList.size());

                for (int i = 0; i < secondList.size(); i++) {

                    HomeGetSecondBody secondBody = secondList.get(i);

                    L.d("DemoListActivity secondBody:" + secondBody);

                }

            }
        });


        /**
         *   use for update time
         */
        String testf = StringUtils.friendlyTime("2016-01-22 09:11:22");
        L.d("WelcomeActivity testf:" + testf);


        GoodsGetSimilarListRequest similarRequest = new GoodsGetSimilarListRequest() {
        };
        RemoteServiceFactory.getInstance().getGoodsService(getApplication()).getSimilarList(similarRequest, new RemoteServiceListenerT<GoodsGetSimilarListResponse>() {
            @Override
            public void okData(GoodsGetSimilarListResponse goodsGetSimilarListResponse) {
                super.okData(goodsGetSimilarListResponse);
                List<GoodsGetSimilarList> SimilarList = goodsGetSimilarListResponse.getGetSimilarList();
                L.d("DemoListActivity tmp.size():" + SimilarList.size());
                for (int i = 0; i < SimilarList.size(); i++) {
                    GoodsGetSimilarList tmp = SimilarList.get(i);
                    String name = tmp.getName();
                    L.d("DemoListActivity name:" + name);
                    List<GoodsGetSimilarListData> data = tmp.getData();
                    for (int j = 0; j < data.size(); j++) {
                        GoodsGetSimilarListData listdata = data.get(j);
                        String bodyhame = listdata.getName();
                        L.d("DemoListActivity bodyhame:" + bodyhame + " getImg:" + listdata.getImg());
                    }
                }
            }
        });


        CommonGetADListRequest adReauest = new CommonGetADListRequest() {
            @Override
            public String givePosition() {
                return "14";
            }

            @Override
            public String giveVirtualCatId() {
                return "";
            }
        };

        RemoteServiceFactory.getInstance().getCommonService(getApplication()).getADList(adReauest, new RemoteServiceListenerT<CommonGetADListResponse>() {


            @Override
            public void okData(CommonGetADListResponse commonGetADListResponse, int fromWhere) {
                super.okData(commonGetADListResponse, fromWhere);


                List<CommonGetADList> adList = commonGetADListResponse.getGetADList();

                L.d("HomeFragment adList.size():" + adList.size());
                for (int i = 0; i < adList.size(); i++) {
                    CommonGetADList tmp = adList.get(0);

                    L.d("HomeFragment getADList tmp.getImg():" + tmp.getImg());

                }

                L.d("HomeFragment getADList commonGetADListResponse:" + commonGetADListResponse.getGetADList());
            }

            @Override
            public void failue(int errorNo, String strMsg) {
                super.failue(errorNo, strMsg);
                L.d("HomeFragment getADList strMsg:" + strMsg);

            }

            @Override
            public void Finish() {
                super.Finish();
                L.d("HomeFragment getADList Finish:");
            }
        });

        HomeGetSpecialsRequest re1 = new HomeGetSpecialsRequest() {
        };
        RemoteServiceFactory.getInstance().getHomeService(getApplication()).getSpecials(re1, new RemoteServiceListenerT<HomeGetSpecialsResponse>() {
            @Override
            public void okData(HomeGetSpecialsResponse homeGetSpecialsResponse, int fromWhere) {
                super.okData(homeGetSpecialsResponse, fromWhere);
                List<HomeGetSpecials> ss = homeGetSpecialsResponse.getGetSpecials();
                L.d("WelcomeActivity ss:" + ss.size() + " fromWhere:" + fromWhere);

            }
        });


        ActivitiesSeckillNoticeRequest ww = new ActivitiesSeckillNoticeRequest() {
            @Override
            public String giveProductId() {
                return "897";
            }

            @Override
            public String giveSecondId() {
                return "693";
            }

            @Override
            public String giveDeviceId() {
                return "1";
            }

            @Override
            public String giveIsSet() {
                return "1";
            }
        };
        RemoteServiceFactory.getInstance().getActivitiesSeckillService(getApplication()).notice(ww, new RemoteServiceListenerT<ActivitiesSeckillNoticeResponse>() {

            @Override
            public void PreStart() {
                super.PreStart();
                L.d("WelcomeActivity seckk PreStart:");
            }


            @Override
            public void okData(ActivitiesSeckillNoticeResponse activitiesSeckillNoticeResponse) {
                super.okData(activitiesSeckillNoticeResponse);
                String meww = activitiesSeckillNoticeResponse.getMessage();
                L.d("WelcomeActivity okData getActivitiesSeckillService meww:" + meww);
            }


            @Override
            public void okData(ActivitiesSeckillNoticeResponse activitiesSeckillNoticeResponse, int fromWhere) {
                super.okData(activitiesSeckillNoticeResponse, fromWhere);
                String meww = activitiesSeckillNoticeResponse.getMessage();
                L.d("WelcomeActivity okData getActivitiesSeckillService meww:" + meww + " fromWhere:" + fromWhere);
            }

            @Override
            public void onMessage(String message) {
                super.onMessage(message);
                L.d("WelcomeActivity message:" + message);
            }

            @Override
            public void failue(int errorNo, String strMsg) {
                super.failue(errorNo, strMsg);
                L.d("WelcomeActivity errorNo:" + strMsg);
            }

            @Override
            public void Finish() {
                super.Finish();
                L.d("WelcomeActivity seckk Finish:");
            }
        });

    }

}
